
package com.feib.soeasy.job.importor;

import java.text.SimpleDateFormat;

import java.util.HashMap;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sourceforge.stripes.integration.spring.SpringBean;




import com.feib.soeasy.job.service.ImportService;
import com.feib.soeasy.model.ColumnValidateError;

import com.feib.soeasy.model.FormatDefinition;
import com.feib.soeasy.model.PayerTemp;
import com.feib.soeasy.model.Payment;

import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.service.PayerTempService;


/**
 * @title (#)PayerTempValidator<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/11/27 create by Jimmy Liu<br>
 */
public class PayerTempValidator extends BaseValidatable  {
    
    private static Map<String, Integer> CODE_MAPPING = new HashMap<String, Integer>();
    static
    {
        CODE_MAPPING.put("1", new Integer(1));
        CODE_MAPPING.put("2", new Integer(2));
        CODE_MAPPING.put("3", new Integer(3));
        CODE_MAPPING.put("4", new Integer(4));
        CODE_MAPPING.put("5", new Integer(5));
        CODE_MAPPING.put("6", new Integer(6));
        CODE_MAPPING.put("7", new Integer(7));
        CODE_MAPPING.put("8", new Integer(8));
        CODE_MAPPING.put("9", new Integer(9));
        CODE_MAPPING.put("0", new Integer(0));
        
        CODE_MAPPING.put("A", new Integer(1));
        CODE_MAPPING.put("B", new Integer(2));
        CODE_MAPPING.put("C", new Integer(3));
        CODE_MAPPING.put("D", new Integer(4));
        CODE_MAPPING.put("E", new Integer(5));
        CODE_MAPPING.put("F", new Integer(6));
        CODE_MAPPING.put("G", new Integer(7));
        CODE_MAPPING.put("H", new Integer(8));
        CODE_MAPPING.put("I", new Integer(9));
        
        CODE_MAPPING.put("J", new Integer(1));
        CODE_MAPPING.put("K", new Integer(2));
        CODE_MAPPING.put("L", new Integer(3));
        CODE_MAPPING.put("M", new Integer(4));
        CODE_MAPPING.put("N", new Integer(5));
        CODE_MAPPING.put("O", new Integer(6));
        CODE_MAPPING.put("P", new Integer(7));
        CODE_MAPPING.put("Q", new Integer(8));
        CODE_MAPPING.put("R", new Integer(9));
        
        CODE_MAPPING.put("S", new Integer(2));
        CODE_MAPPING.put("T", new Integer(3));
        CODE_MAPPING.put("U", new Integer(4));
        CODE_MAPPING.put("V", new Integer(5));
        CODE_MAPPING.put("W", new Integer(6));
        CODE_MAPPING.put("X", new Integer(7));
        CODE_MAPPING.put("Y", new Integer(8));
        CODE_MAPPING.put("Z", new Integer(9));
        

        CODE_MAPPING.put("*", new Integer(0));
    }

    
    //private Set<Long> payerTempPks = new HashSet<Long>();
    private  Set<String> hasPayerTemp= new HashSet<String>();
    
    protected SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    
   
    private PayerTempService payerTempService;
   
    
    
    
    
    
    /**
     * @param uploadFile
     * @param formatDefinition
     * @param columnValidateErrors
     *
     * 
     */
    public PayerTempValidator(UploadFile uploadFile, FormatDefinition formatDefinition, List<ColumnValidateError> columnValidateErrors, ImportService importService,PayerTempService payerTempService) {
        super(uploadFile, formatDefinition, columnValidateErrors, importService );
        this.setPayerTempService(payerTempService);
        
    }
    
    
    

    
    /* (non-Javadoc)
     * @see com.feib.soeasy.job.BaseValidatable#Validate(com.feib.soeasy.model.Payment)
     */
  
      
   
    

    public boolean payerTempValidate(PayerTemp payerTemp, long lineNumber) {
        boolean passed = true;
        
        // Rule01: 繳款人代號是否重複
        if (this.hasPayerTemp(payerTemp))
        {
            passed = false;
            super.addColumnValidateError(lineNumber, super.getColumnTitle("PAYER_NO"), "繳款人代號重覆("+payerTemp.getPayerNo()+")", null);

        }
       
       //額外檢查
      /*  if (passed)
            passed = doExtraValidate(payerTemp, lineNumber);*/
      
       
        return passed;
    }
     
    
    /**
     * hasPayerTemp  檔案中是否有重複 PayerNo
     * @param payerTemp
     * @return
     */
    public boolean hasPayerTemp(PayerTemp payerTemp)
    {
        // step1: 檔案中是否有重複
        String  checkNo  =payerTemp.getPayerNo().trim();
        boolean reulst = hasPayerTemp.contains(checkNo);
        
        PyerImportJob.logger.debug("checkNo {}",checkNo);
        PyerImportJob.logger.debug("getPayerTempService {}",getPayerTempService());
        PyerImportJob.logger.debug("reulst {}",reulst);
        
        
        
        if (!reulst)
        {
            hasPayerTemp.add(checkNo);
            
            PyerImportJob.logger.debug("hasPayerTemp ALL {}",hasPayerTemp);
            for(String aa:hasPayerTemp){
                PyerImportJob.logger.debug("hasPayerTemp {}",aa);
            }
            
            PyerImportJob.logger.debug("getPayerTempService {}",getPayerTempService());
            PyerImportJob.logger.debug("getGroupPk() {}",payerTemp.getGroup().getGroupPk());
            
            // step2 : 檔案中無重複，再檢查資料庫 
           PyerImportJob.logger.debug("getPayerTempIsExistByGroupPk {}", getPayerTempService().getPayerTempIsExistByGroupPk(payerTemp.getGroup().getGroupPk(), payerTemp.getPayerNo()));
           return    getPayerTempService().getPayerTempIsExistByGroupPk(payerTemp.getGroup().getGroupPk(), payerTemp.getPayerNo());
          // return getPayerTempService().getPayerTempIsExist(payerTemp.getGroup().getGroupNo(), payerTemp.getPayerNo());
        }
        
        return reulst;
    }


    /* (non-Javadoc)
     * @see com.feib.soeasy.job.importor.BaseValidatable#validate(com.feib.soeasy.model.Payment, long)
     */
    @Override
    public boolean validate(Payment payment, long lineNumber) {
        // TODO Auto-generated method stub
        return false;
    }





    /**
     * @param payerTempService the payerTempService to set
     */
    private void setPayerTempService(PayerTempService payerTempService) {
        this.payerTempService = payerTempService;
    }





    /**
     * @return the payerTempService
     */
    private PayerTempService getPayerTempService() {
        return payerTempService;
    }
    
    
    
    

}
